package stancel.peter.smslogger;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.List;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * Trieda, ktora obsahuje globalne premenne a metody,
 * ktore su potrebne pre komponenty aplikacie.
 * @author Peter Stancel
 */
public class SMSLogger extends Application{

	private final String fileName = "SmsLog.dat";

	@Override
	public void onCreate() {
		super.onCreate();

		/**
		 * Nastavy predvolene hodnoty z <b>preferences.xml</b>.
		 * Spravi to len pri prvom spusteni aplikacie.
		 */
		PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
	}

	/**
	 * Vymaze vsetky subory so serializovanymi listami obsahujucimi smsky.
	 * Spusti metodu, ktora nastavy <i>fileNumber</i> na 1.
	 */
	public void deleteAllSms(){
		int i = 1;
		while(deleteFile(i + fileName)){
			i++;
		}
		setFileNumber(1);
	}

	/**
	 * Nacita serializovany list smsiek zo suboru urceneho pomocou <i>fileNumber</i>.
	 * @param fileNumber Urcuje cislo suboru, ktory ma byt nacitany.
	 * @return List<Sms> 
	 */
	public synchronized List<Sms> loadFromFile(int fileNumber) {
		List<Sms> list = new ArrayList<Sms>();
		try {
			FileInputStream fis = openFileInput(fileNumber + fileName);
			ObjectInputStream ois = new ObjectInputStream(fis);
			list = (List<Sms>) ois.readObject();

			fis.close();
			ois.close();
		} catch (FileNotFoundException e) {
			// Subor neexistuje.
			// Moze to nastat ak sa spusti aplikacia prvykrat.
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * Serializuje a ulozi <i>list</i> smsiek do suboru urceneho pomocou <i>fileNumber</i>.
	 * Subor bude mat meno <i>fileNumber</i> + "SmsLog.dat".
	 * @param fileNumber Urcuje cislo suboru, do ktoreho sa ma ulozit <i>list</i>.
	 * @param list List smsiek, ktory sa ma ulozit do suboru.
	 */
	public synchronized void saveToFile(int fileNumber, List<Sms> list) {
		try {
			FileOutputStream fos = openFileOutput(fileNumber + fileName, Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(list);

			fos.flush();
			oos.flush();
			fos.close();
			oos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Ziska maximalne cislo <i>fileNumber</i> ulozene v <b>SharedPreferences</b>,
	 * ktore reprezentuje subor s poslednymi prijatymi SMSkami.
	 * @return fileNumber
	 */
	public int getMaxFileNumber(){
		SharedPreferences settings = getSharedPreferences("preferences", MODE_PRIVATE);
		return settings.getInt("smslogger_preferences_file_number", 1);
	}

	/**
	 * Ulozi <i>fileNumber</i> do <b>SharedPreferences</b>.
	 * Toto cislo reprezentuje maximalny subor, ktory existuje.
	 * @param fileNumber Urcuje cislo maximalneho suboru, ktory existuje.
	 */
	public void setFileNumber(int fileNumber){
		SharedPreferences settings = getSharedPreferences("preferences", MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt("smslogger_preferences_file_number", fileNumber);
		editor.commit();
	}
}